package com.lu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.mapper.SysMechantTypesMapper;
import com.lu.model.entity.SysMechantTypes;
import com.lu.model.exception.LuBootAssert;
import com.lu.service.SysMechantTypesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商户分类 服务实现类
 * </p>
 *
 * @author zhanglu
 * @since 2022-09-19
 */
@RequiredArgsConstructor
@Service
public class SysMechantTypesServiceImpl extends ServiceImpl<SysMechantTypesMapper, SysMechantTypes> implements SysMechantTypesService {

    @Override
    public List<SysMechantTypes.TreeNodes> getListByPid(Long pid) {
        QueryWrapper<SysMechantTypes> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(pid)){
            queryWrapper.eq("id", pid).or().eq("pid", pid);
        }
        queryWrapper.orderByAsc("id");
        List<SysMechantTypes> objs = this.baseMapper.selectList(queryWrapper);
        return SysMechantTypes.treeNodes(objs, pid);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysMechantTypes add(SysMechantTypes mechantTypes) {
        LuBootAssert.isTrue(ObjectUtil.isNotEmpty(mechantTypes.getName()), "请输入商户类型名称");
        List<String> cascadeIds = new ArrayList<>();
        if(ObjectUtil.isNotEmpty(mechantTypes.getPid()) && mechantTypes.getPid() != 0L){
            //构建级联id，pid0->pid1->pid2->id
            SysMechantTypes pObj = this.baseMapper.selectById(mechantTypes.getPid());
            LuBootAssert.isTrue(pObj != null, "请重新选择父级类型");
            if(ObjectUtil.isEmpty(pObj.getCascadeIds()) || pObj.getCascadeIds().equals("0")){
                cascadeIds.add(pObj.getId().toString());
            }else {
                cascadeIds = Arrays.stream(pObj.getCascadeIds().split(StrUtil.COMMA)).collect(Collectors.toList());
            }
        }
        this.baseMapper.insert(mechantTypes);
        cascadeIds.add(mechantTypes.getId().toString());
        SysMechantTypes updateObj = new SysMechantTypes();
        updateObj.setId(mechantTypes.getId());
        updateObj.setCascadeIds(StrUtil.join(StrUtil.COMMA, cascadeIds));
        this.baseMapper.updateById(updateObj);
        return this.baseMapper.selectById(mechantTypes.getId());
    }

    @Override
    public SysMechantTypes updateSysMechantTypes(SysMechantTypes mechantTypes) {
        LuBootAssert.isTrue(ObjectUtil.isNotEmpty(mechantTypes.getId()), "请输入修改标识");
        if(ObjectUtil.isNotEmpty(mechantTypes.getPid()) && mechantTypes.getPid() != 0L){
            List<String> cascadeIds = new ArrayList<>();
            //构建级联id，pid0->pid1->pid2->id
            SysMechantTypes pObj = this.baseMapper.selectById(mechantTypes.getPid());
            LuBootAssert.isTrue(pObj != null, "请重新选择父级类型");
            if(ObjectUtil.isEmpty(pObj.getCascadeIds()) || pObj.getCascadeIds().equals("0")){
                cascadeIds.add(pObj.getId().toString());
            }else {
                cascadeIds = Arrays.stream(pObj.getCascadeIds().split(StrUtil.COMMA)).collect(Collectors.toList());
            }
            cascadeIds.add(mechantTypes.getId().toString());
            mechantTypes.setCascadeIds(StrUtil.join(StrUtil.COMMA, cascadeIds));
        }
        this.baseMapper.updateById(mechantTypes);
        return this.baseMapper.selectById(mechantTypes.getId());
    }
}
